<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<script type="text/javascript">
 $(function(){
	 
	 $('#indexContent').datagrid({
		 pageList:[10]
	 	});
	 
	 url = "${pageContext.request.contextPath}/content/addContent.html";
	 $('#newContent').bind('click',function(){
			$('#dlgcontent').dialog('open');
			$(':input').val('');
			tinyMCE.activeEditor.setContent('');
		 });

	 $('#saveContent').bind('click',function(){
		     $('#content').val(tinyMCE.activeEditor.getContent(''));
		     if ($('#id').val() != "") {
		    	 url = "${pageContext.request.contextPath}/content/modifyContent.html";
			   } else {
				   url = "${pageContext.request.contextPath}/content/addContent.html";
				}
			 $('#fmcontent').form('submit',{
				 url:url,
				 onSubmit: function(){
				 	if (($.trim($('#content').val()) == "")) {
				 		 showMessage("错误","内容必须填写!");
				 		 return false;
					 } else {
						 return $(this).form('validate');
					 }
				 },
				 success: function(result){
					 var result = eval('('+result+')');
					 if (result.errorMsg){
						 showMessage("错误",result.errorMsg);
					 } else {
						 showMessage("成功","发布成功");
						 $('#dlgcontent').dialog('close'); // close the dialog
						 $('#indexContent').datagrid('reload'); // reload the user data
					 }
				 	}
				 });
	});

 })
 
 
 function showMessage(title,message) {
	 $.messager.show({
		 title: title,
		 msg: message
		 });
}

 function editCotent(id,content,title){
			 $('#id').val(id);
			 $('#title').val(title);
			 tinyMCE.activeEditor.setContent(content);
			 $('#dlgcontent').dialog('open');
	 }

function deleteContent(id){
	 if (id){
		$.getJSON("${pageContext.request.contextPath}/content/deleteContent.html",
				 {"id":id},
				function(result){
					 if (result.errorMsg){
						 showMessage("错误","result.errorMsg");
					 } else {
						 showMessage("成功","删除成功");
						 $('#dlgcontent').dialog('close'); // close the dialog
						 $('#indexContent').datagrid('reload'); // reload the user data
					 }
				});
	 }
}
function formatLen(val,row) {
	 if (val.length > 100){
			 return val.substring(0,100);
		 } else {
			 return val;
		 }
}

function showButtons(val,row){
	var e = "<input type='button' class='icon-edit' border='0' onclick='editCotent("+row.id+",\""+row.content+"\",\""+row.title+"\")'/>";
	var d = "<input type='button' class='icon-delete' border='0' onclick='deleteContent("+ row.id +")'/>";
	return e + " " + d;
}
 </script>
 <table id="indexContent" class="easyui-datagrid" style="width:auto;height:600px"
		url="${pageContext.request.contextPath}/content/viewContentsJson.html"
		singleSelect="true"
		pagination="true"
		pagePosition="both"
		fitColumns="true"t56y7
		toolbar="#toolbarcontent"
		>
    <thead>
	    <tr>
		    <th data-options="field:'title',width:100">标题</th>
		    <th data-options="field:'publisher',width:100">发布者</th>
		    <th data-options="field:'content',width:200,formatter:formatLen">内容</th>	
		    <th data-options="field:'createtime',width:100">发布时间</th>	    
		   	<th data-options="field:'opt',width:100,formatter:showButtons">操作</th>	        
	    </tr>
    </thead>
   </table>
   
 <div id="toolbarcontent">
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" id="newContent">添加数据</a>
</div>  


 <div id="dlgcontent" class="easyui-dialog" style="width:1000px;height:500px;padding:10px 20px"
	closed="true" buttons="#dlg-buttoncontent">
	 <div class="ftitle">内容发布</div>
	<form id="fmcontent" method="post" novalidate>
		<input type="hidden" name="content" id="content"/>
		<input type="hidden" name="id" id="id" value=""/>
		<div class="fitem">
			<label>标题：</label>
			<input type="text" name="title" id="title" class="easyui-validatebox"  data-options="required:true"/>
		</div>
		<div class="fitem">
            <textarea id="editor1" name="editor1" rows="10" cols="80">
            </textarea>
		</div>
	</form>
</div>
 <div id="dlg-buttoncontent">
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" id="saveContent">保存</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlgcontent').dialog('close')">取消</a>
</div>
<script type="text/javascript">
tinymce.init({
    selector: "textarea"
 });

</script>
